//
//  harvestAddress.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/22.
//  Copyright © 2016年 wl. All rights reserved.
//

import SVProgressHUD

class harvestAddressVC: UIViewController, UITextFieldDelegate {
    var id : Int = 0
    var type: String = ""
    var msg: regiestInfo?{
        didSet{
            
        }
    }
   
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var postCode: UITextField!
    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var tel: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
       self.address.delegate = self
        self.postCode.delegate = self
        self.name.delegate = self
        self.tel.delegate = self
        if ApplicationDelegate.isCompleteDefaultAddress(){
            self.address!.text! = (NSUserDefaults.standardUserDefaults().objectForKey(address_key)) as! String
            self.postCode!.text! = (NSUserDefaults.standardUserDefaults().objectForKey(postCode_key)) as! String
            self.name!.text! = (NSUserDefaults.standardUserDefaults().objectForKey(name_key)) as! String
            self.tel!.text! = (NSUserDefaults.standardUserDefaults().objectForKey(address_key)) as! String
        }
        
        
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        //SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    func textFieldDidEndEditing(textField: UITextField){
        if textField == self.address{
            
            NSUserDefaults.standardUserDefaults().setObject(self.address!.text!, forKey: address_key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        if textField == self.postCode{
            
             NSUserDefaults.standardUserDefaults().setObject(self.postCode!.text!, forKey: postCode_key)
             NSUserDefaults.standardUserDefaults().synchronize()
        }
        if textField == self.name{
            
           NSUserDefaults.standardUserDefaults().setObject(self.name!.text!, forKey: name_key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        if textField == self.tel{
            NSUserDefaults.standardUserDefaults().setObject(self.tel.text!, forKey: tel_key)
            
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
    }
    @IBAction func getIntergatNum(sender: UIButton){
        if NSUserDefaults.standardUserDefaults().objectForKey(address_key) == nil{
            SVProgressHUD.showErrorWithStatus("请填写地址")
           
            return

        }
        if NSUserDefaults.standardUserDefaults().objectForKey(postCode_key) == nil{
            SVProgressHUD.showErrorWithStatus("请填写邮政编码")
           
            return
        }
        if NSUserDefaults.standardUserDefaults().objectForKey(name_key) == nil{
            SVProgressHUD.showErrorWithStatus("请填写姓名")
           
            return
        }
        if NSUserDefaults.standardUserDefaults().objectForKey(tel_key) == nil{
             SVProgressHUD.showErrorWithStatus("请填写电话")
           
            return
        }
        if self.type == "积分"{
            self.saveAddress()
            self.checkIntegralNum()
        }else{
            self.saveAddress()
        }
    }
    
    
    //MARK:networking
    func saveAddress(){
        DataTool.saveAddress(self.address.text!, postcode: self.postCode.text!, name: self.name.text!, tel: self.tel.text!,completionHandler: { (newModel) in
            guard newModel != nil else {
                return
            }
            self.msg = newModel
           NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isCompleteDefaultAddress")
            
            NSUserDefaults.standardUserDefaults().synchronize()
            SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
           
            self.navigationController?.popToRootViewControllerAnimated(true)
        })
    }
    func checkIntegralNum(){
        DataTool.checkIntegral(String(id), completionHandler:
            { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                if newDataes.msg == "保存成功"{
                    SVProgressHUD.showSuccessWithStatus(newDataes.msg)
                    self.navigationController?.popToRootViewControllerAnimated(true)
                }else{
                    SVProgressHUD.showErrorWithStatus(newDataes.msg)
                }
        })

    }

}
